package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.PercentageItem;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.getAgentConditionMarkupConfigListBean;
import com.ymatou.iapi.godmch.parameter.setAgentConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getAgentConditionMarkupConfigListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_agent_priceWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_agent_priceWapper;
/**
*代理价按条件自动加价配置列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentConditionMarkupConfigList{
	private static getAgentConditionMarkupConfigListBean bean;
	private static getAgentConditionMarkupConfigListCall call;
	agent_typeWapper agentTypeWapper = new agent_typeWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("代理价按条件自动加价配置列表");
	}

	@Before
	public void caseUp() {
		bean=new getAgentConditionMarkupConfigListBean();
		call=new getAgentConditionMarkupConfigListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private markup_config_agent_price selectConfig(Integer mchId,Integer... markupId) throws InterruptedException
	{
		markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
		markup_config_agent_priceExample example=new markup_config_agent_priceExample();
		example.createCriteria().andMch_idEqualTo(mchId);
		if(markupId.length>0)
			example.createCriteria().andMarkup_idEqualTo(markupId[0]);
		
		example.setOrderByClause(" create_time desc");
		markup_config_agent_price config=markupConfig.selectByExample(example).get(0);
		return config;
	}
	private void updateTimeConfig(Integer mchId) throws InterruptedException
	{
		   //修改update_time 因为相同的agentid+suppelierId 1分钟内无法修改
		   markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
		   markup_config_agent_priceExample example=new markup_config_agent_priceExample();
		   example.createCriteria().andMch_idEqualTo(mchId);
		   markup_config_agent_price config=markupConfig.selectByExample(example).get(0);
		   config.setUpdate_time(Date.from((LocalDateTime.now().plusDays(-1).atZone(ZoneId.systemDefault()).toInstant())));
		   markupConfig.updateByPrimaryKey(config);
	}
	private void check_verify(Integer mchId) throws InterruptedException {
		
		 Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		 JSONArray jArray=call.getList();
		 Logger.verifyEquals(jArray.size(), call.getTotal(), "验证total与返回的一致");

			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(mchId);
			example.setOrderByClause(" create_time desc");
			List<markup_config_agent_price>  configs=markupConfig.selectByExample(example);
			for(int i=0;i<configs.size();i++){
				markup_config_agent_price config=configs.get(i);
				JSONObject job=jArray.getJSONObject(i);
				 Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
				
			//	 Logger.verifyEquals(Integer.parseInt(job.get("markupType").toString()), config.getMarkup_type(), "验证数据库中的Markup_type与返回的一致");
			//	 Logger.verifyEquals(new BigDecimal(job.get("percentage").toString()).stripTrailingZeros().toPlainString(), config.getPercentage().stripTrailingZeros().toPlainString(), "验证数据库中的percentage与返回的一致");
				 Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
			//	 Logger.verifyEquals(Integer.parseInt(job.get("onSaleType").toString()), config.getOn_sale_type(), "验证数据库中的onSaleType与返回的一致");
				 Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
			}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigList_001() {
		Logger.start(true,"代理价按条件自动加价配置列表-一条类型-返回成功");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			check_verify( setAgentConditionMarkupConfigBean.getMchId()) ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigList_002() {
		Logger.start(true,"代理价按条件自动加价配置列表-设置两条类型-返回成功");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			//创建代理类型
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(setAgentConditionMarkupConfigBean.getMchId());
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			//查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", setAgentConditionMarkupConfigBean.getMchId());
			List<agent_type> list = agentTypeWapper.selectby(map);
			//代理类型1
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			//代理类型2
			item=new PercentageItem();
			item.setAgentTypeId(list.get(1).getAgentType());
			item.setPercentage(new BigDecimal(20));
			items.add(item);
			setAgentConditionMarkupConfigBean.setPercentageList(items);
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			
			bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			check_verify( setAgentConditionMarkupConfigBean.getMchId()) ;	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getAgentConditionMarkupConfigList_003() {
		Logger.start(true,"代理价按条件自动加价配置列表-中间一页显示-返回成功(此case中间等待时间,一个商户创建一条自动加价 中间要等待 1分钟后再创建)");
		try {
			//第一条
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean1=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			Integer mchId=setAgentConditionMarkupConfigBean1.getMchId();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean1);
			Thread.sleep(50000);
			//第二条
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean2=GodmchManageCallService.initSetAgentConditionMarkupConfig(mchId);
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean2);
			Thread.sleep(50000);
			//第三条
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean3=GodmchManageCallService.initSetAgentConditionMarkupConfig(mchId);
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean3);
			
			bean.setMchId(setAgentConditionMarkupConfigBean1.getMchId());
			bean.setPageSize(1);
			bean.setPageIndex(2);
			call.setData(bean);
			call.callService();			

			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			JSONArray jArray=call.getList();
			Logger.verifyEquals(3, call.getTotal(), "验证total与返回的一致");
			
			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample	 example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(mchId);
			example.setOrderByClause(" create_time desc");
			List<markup_config_agent_price>  configs=markupConfig.selectByExample(example);
			
			markup_config_agent_price config=configs.get(0);
			JSONObject job=jArray.getJSONObject(0);
			Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
			Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
			Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getAgentConditionMarkupConfigList_004() {
		Logger.start(true,"代理价按条件自动加价配置列表-最后一页显示-返回成功(此case中间等待时间,一个商户创建一条自动加价 中间要等待 1分钟后再创建)");
		try {
			//第一条
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean1=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			Integer mchId=setAgentConditionMarkupConfigBean1.getMchId();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean1);
			Thread.sleep(50000);
			//第二条
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean2=GodmchManageCallService.initSetAgentConditionMarkupConfig(mchId);
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean2);
			
			bean.setMchId(setAgentConditionMarkupConfigBean1.getMchId());
			bean.setPageSize(1);
			bean.setPageIndex(2);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			JSONArray jArray=call.getList();
			Logger.verifyEquals(2, call.getTotal(), "验证total与返回的一致");
			
			markup_config_agent_priceWapper markupConfig=new markup_config_agent_priceWapper();
			markup_config_agent_priceExample	 example=new markup_config_agent_priceExample();
			example.createCriteria().andMch_idEqualTo(mchId);
			example.setOrderByClause(" create_time desc");
			List<markup_config_agent_price>  configs=markupConfig.selectByExample(example);
			
			markup_config_agent_price config=configs.get(1);
			JSONObject job=jArray.getJSONObject(0);
			Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
			Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
			Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentConditionMarkupConfigList_005() {
		Logger.start(true,"代理价按条件自动加价配置列表-商户id为空-返回失败");
		try {
			setAgentConditionMarkupConfigBean setAgentConditionMarkupConfigBean=GodmchManageCallService.initSetAgentConditionMarkupConfig();
			GodmchManageCallService.setAgentConditionMarkupConfigCall(setAgentConditionMarkupConfigBean);
			
			//bean.setMchId(setAgentConditionMarkupConfigBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}